<?php

namespace app\admin\command;

use think\Config;
use think\Controller;
use think\console\input\Option;
use think\Db;

class FormField extends Controller
{
    protected $model = null;
    protected $field_array = array();
    protected $formName = null;

    /*
     * 自动生成表单表和字段
     */
    public function __construct($field)
    {
        $this->field_array = $field;

        if(is_array($field) && isset($field['form_id'])){
            $formName = Db::name('form')->field('table')->find($field['form_id']);
            $this->formName = $formName['table'];

            if($this->formName){
                $form = Db::query('show tables like "fa_form_'.$this->formName.'"');
                if(!$form){
                    $this->creatTable($formName['table']);
                }
            }
            if(isset($field['oldFieldTable']) && $field['oldFieldTable'] != $field['form_id']){
                $formName = Db::name('form')->field('table')->find($field['oldFieldTable']);
                $this->oldFieldTable = $formName['table'];
                $this->deleteOldTableField();
            }
        }
    }

    /*
     * 创建数据表和相应字段
     */
    protected function creatTable()
    {
        $sql = 'CREATE TABLE IF NOT EXISTS `fa_form_'.$this->formName.'`(
           `id` INT UNSIGNED AUTO_INCREMENT,
           `createtime` INT(10),
           `weigh` INT(10),
           PRIMARY KEY ( `id` )
        )ENGINE=InnoDB DEFAULT CHARSET=utf8;
        ';
        Db::query($sql);
        return;
    }

    /*
     *  生成字段
     */
    public function creatField()
    {
        // alter table bulletin add citycode varchar(6) not null default 0;
        if($this->field_array['required'] == 1){
            $isRequired = ' NOT NULL ';
        }else{
            $isRequired = ' NULL ';
        }

        // 默认数据，考虑到字段类型的默认数据不同，先注释
        //        if($this->field_array['default'] == ''){
//            $isDefault = ' ';
//        }else{
//            $isDefault = ' DEFAULT `'.$this->field_array['default'] .'` ';
//        }

        if($this->field_array['notes'] == ''){
            $isComment = ' ';
        }else{
            $isComment = ' COMMENT "'.$this->field_array['notes'] .'" ';
        }

        $sql = 'ALTER TABLE `fa_form_' . $this->formName . '` ADD COLUMN `' . $this->field_array['name'] . '` '.
            $this->field_array['type'] . '('. $this->field_array['length'] .')'. $isRequired.
            /*$isDefault.*/$isComment;
        ;
        Db::query($sql);
        return;
    }

    /*
     *  修改字段
     */
    public function changeField()
    {
        // 修改字段sql
        // ALTER TABLE `fa_form_rege`
        // CHANGE COLUMN `post` `post_22`  varchar(200)
        // CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '123456' COMMENT '6666' AFTER `id`;

        // 检查字段是否存在sql
        // describe `fa_form_bottom_form` `username

        $sql = 'describe `fa_form_' . $this->formName . '` `'.$this->field_array['oldFieldName'] .'`';

        $is_existence = Db::query($sql);

        if(count($is_existence) == 0){
            $this->creatField();
            return;
        }

        if($this->field_array['required'] == 1){
            $isRequired = ' NOT NULL ';
        }else{
            $isRequired = ' NULL ';
        }

        // 默认数据，考虑到字段类型的默认数据不同，先注释

//        if($this->field_array['default'] == ''){
//            $isDefault = ' ';
//        }else{
//            $isDefault = ' DEFAULT `'.$this->field_array['default'] .'` ';
//        }

        if($this->field_array['notes'] == ''){
            $isComment = ' ';
        }else{
            $isComment = ' COMMENT "'.$this->field_array['notes'] .'" ';
        }

        $sql = 'ALTER TABLE `fa_form_' . $this->formName . '` CHANGE COLUMN `' . $this->field_array['oldFieldName'] . '` `'.
            $this->field_array['name'] . '` ' . $this->field_array['type'] . '('. $this->field_array['length'] .')'. $isRequired.
            /*$isDefault.*/$isComment;
        ;
        Db::query($sql);
        return;
    }

    /*
    *  删除字段
    */
    public function deleteField()
    {
        // ALTER TABLE `fa_form_rege`
        // DROP COLUMN `password_o`;

        $sql = 'ALTER TABLE `fa_form_' . $this->formName . '` DROP COLUMN `' . $this->field_array['name'] . '`';
        Db::query($sql);
        return;
    }

    /*
    *  删除旧表字段
    */
    public function deleteOldTableField()
    {
        // ALTER TABLE `fa_form_rege`
        // DROP COLUMN `password_o`;

        $sql = 'ALTER TABLE `fa_form_' . $this->oldFieldTable . '` DROP COLUMN `' . $this->field_array['oldFieldName'] . '`';
        Db::query($sql);
        return;
    }

}
